<?php
namespace App\Controllers\Coupons;
use App\Models\Coupons\Coupons;
use App\Models\Coupons\CouponsMy;
use App\Libs\Coupons\Couponslib;
/**
*	获取优惠券信息
*/
class GetCoupons extends \App\Libs\Base\BaseAction
{
	protected function beforeRun() {
         parent::beforeRun();
         $this->needLogin = TRUE;
    }
	
	public function execute() {
		$user = $this->session->user;
		$page = (int)$this->request->get('page');//页数
		$size = (int)$this->request->get('size');//展示条数
		$status = (int)$this->request->get('status');//状态
		
		//查询
		$params['uid'] = $user->id;
		if( !empty($page)  ){
			$params['page'] = $page;
		}
		if( !empty($size)  ){
			$params['size'] = $size;
		}
		if(in_array($status,[0,1,2])){
			$params['status'] = $status;
		}else{
			return \App\Libs\Views\Json::view( 1 , '参数有误', NULL);
		}
		$couponsList = Couponslib::getCoupons( $params );
		if( !empty( $couponsList ) ){
			foreach( $couponsList as $k=>$v ){
				$couponsList[$k]['start_time'] = date("Y/m/d",strtotime($v['start_time']));
				$couponsList[$k]['end_time'] = date("Y/m/d",strtotime($v['end_time']));
				//转换金额：
                $couponsList[$k]['price'] = substr($v['price'],0,strpos($v['price'],'.'));
			}
			//返回成功数据
			return \App\Libs\Views\Json::view( 0 , '成功', array_values($couponsList));
		}else{
			//返回成功数据
			return \App\Libs\Views\Json::view( 0 , '成功', []);
		}
		
		
	}
}
